package GMDPRE

import "fmt"

type ProxyNode struct {
	id BN
}

func (node *ProxyNode) GetId() *BN {
	return &node.id
}

func (node *ProxyNode) Init() {
	if BnCmp(node.GetId(), &Bn0, Sm2CurveDigit) != 0 {
		return
	}

	GenBnLessN(node.GetId())
}

// ReEnCapsulate 重加密
// 29次模乘，8次求逆，4次点乘，1次点加，1次哈希
func (node *ProxyNode) ReEnCapsulate(fBytes []uint8, cBytes []uint8, PubKeyBytes []uint8) []uint8 {
	var capTmp Capsule
	var frag Frag

	capTmp.fromBytes(cBytes)
	frag.fromBytes(fBytes)

	if !capTmp.Check(PubKeyBytes) || BytesEqual(frag.id, BnToBytes(&node.id, Sm2CurveDigit), Sm2CurveSize) != 1 {
		fmt.Println("Capsule Check failed!!!!!!!!!!!!")
	}

	var cFrag CFrag
	NumMulAffP2AffP(&cFrag.E1, &frag.rk, &capTmp.E) // 8次模乘，2次求逆，1次点乘
	//NumMulAffP2AffP(&cFrag.V1, &frag.rk, &capTmp.V) // 8次模乘，2次求逆，1次点乘
	cFrag.id = frag.id
	cFrag.XA = frag.XA

	return cFrag.toBytes()
}
